<?php 
global $_W,$_GPC;
$title = "完成订单";
//订单编号
$id = intval($_GPC['id']);
load()->model('mc');

$sql = "SELECT * FROM ".tablename('imeepos_runner3_tasks')." WHERE id = :id";
$params = array(':id'=>$id);
$task = pdo_fetch($sql,$params);

if(!empty($task)){
	if($task['status'] >= 2){
		$return = array();
		$return['result'] =2;
		$return['message'] = '此订单已有人接单，不可删除！';
		die(json_encode($return));
	}else if($task['status'] == 1){
		
		if($task['type'] == 3){
			//帮我买
			$sql = "SELECT * FROM ".tablename('imeepos_runner3_buy')." WHERE taskid = :taskid";
			$params = array(':taskid'=>id);
			$detail = pdo_fetch($sql,$params);
			$fee = $detail['freight'];
		}else{
			$sql = "SELECT * FROM ".tablename('imeepos_runner3_detail')." WHERE taskid = :taskid";
			$params = array(':taskid'=>$id);
			$detail = pdo_fetch($sql,$params);
			$fee = $detail['total'];
		}
		
		$uid = mc_openid2uid($task['openid']);
		mc_credit_update($uid, 'credit2',$fee,array($uid, '撤销任务退换', 0, 0));
		
		//应退金额 $fee
		pdo_delete('imeepos_runner3_tasks',array('id'=>$id));
		pdo_delete('imeepos_runner3_recive',array('taskid'=>$id));
		pdo_delete('imeepos_runner3_detail',array('taskid'=>$id));
		pdo_delete('imeepos_runner3_buy',array('taskid'=>$id));
		
		$return = array();
		$return['result'] =0;
		$return['message'] = '恭喜您，操作成功！退换金额'.$fee.'已余额到账';
		$return['detail'] = $detail;
		$return['task'] = $task;
		die(json_encode($return));
	}else{
		pdo_delete('imeepos_runner3_tasks',array('id'=>$id));
		pdo_delete('imeepos_runner3_recive',array('taskid'=>$id));
		pdo_delete('imeepos_runner3_detail',array('taskid'=>$id));
		pdo_delete('imeepos_runner3_buy',array('taskid'=>$id));
		
		$return = array();
		$return['result'] =0;
		$return['message'] = '此任务尚未支付，已直接清除';
		die(json_encode($return));
	}
}else{
	$return = array();
	$return['result'] =3;
	$return['message'] = '订单不存在或已删除！';
	die(json_encode($return));
}